import { getWechatAuthUrl, validateWechatAuth, getWechatList, getWechatIsAuth, getWechatMenu, saveWechatMenu, delWechatMenu, getWechatInfo,
         getSubscribeReply, setSubscribeReply, getSourceMoreInfo, getDefaultReply, setDefaultReply, setIfSubReply,
         setIfDefaultReply, getReplyTail, setReplyTail, setIfReplyTail, getKeywordReply, setKeywordReply, delKeywordReply,
         getKefuList, getKefuListOnline, addKefuAccount, bindKefuAccount, setKefuAvatar, setKefuInfo, setAdminInfo,
         delKefuAccount, getWxNewsMediaid, getWxNewsByMediaid, getWxMenuSource } from '@/service/getData'
import * as type from '@/store/mutation-types'
const state = {
  // 公众号列表
  wechatAuthUrl: '', // 微信授权地址
  wechatAuth: false, // 是否微信授权成功
  wechatList: { // 微信公众号列表
    list: [],
    page: {}
  },
  // 自定义菜单
  wechatDetail: {}, // 当前微信公众号
  wechatMenu: [], // 微信公众号菜单
  curMenu: {}, // 当前选中的菜单
  menuIndex: null, // 选中的菜单index
  subMenuIndex: null, // 选中的子菜单
  // 自动回复
  subscribeReply: {
    msgType: 'text',
    content: ''
  }, // 关注回复
  defaultReply: {
    msgType: 'text',
    content: ''
  }, // 消息默认回复
  keywordReply: { // 关键词回复列表
    list: [],
    page: {}
  },
  replyTail: { // 回复小尾巴
    content: '',
    status: 0
  },
  // 客服管理
  kefuList: []
}
const actions = {
  // 公众号列表
  async getWechatAuthUrl ({commit, state}, payload) {
    let res = await getWechatAuthUrl(payload)
    commit(type.GET_WECHAT_AUTH_URL, res.data.data)
  },
  async validateWechatAuth ({commit, state}, payload) {
    let res = await validateWechatAuth(payload)
    commit(type.SET_WECHAT_AUTH, res.data.code)
  },
  async getWechatList ({commit, state}, payload) {
    let res = await getWechatList(payload)
    if (res.data.data.list.length > 0) {
      const promises = []
      res.data.data.list.forEach((item, index) => {
        const promise = getWechatIsAuth(item.appid).then(resl => {
          let resTxt = ''
          if (resl.data.data) {
            resTxt = '是'
          } else {
            resTxt = '否'
          }
          item.authRes = resTxt
        })
        promises.push(promise)
      })
      await Promise.all(promises)
      commit(type.SET_WECHAT_LIST, res.data.data)
    } else {
      commit(type.SET_WECHAT_LIST, res.data.data)
    }
    return res
  },
  async getWechatIsAuth ({commit, state}, appid) {
    let res = await getWechatIsAuth(appid)
    return res
  },
  // 自定义菜单
  async getWechatInfo ({commit, state}, appid) {
    let res = await getWechatInfo(appid)
    commit(type.SET_WECHAT_DETAIL, res.data.data)
    commit(type.SET_BREAD_TAIL, res.data.data.nickName)
  },
  async getWechatMenu ({commit, state}, appid) {
    let res = await getWechatMenu(appid)
    commit(type.SET_WECHAT_MENU, res.data.data.menu.button)
    return res
  },
  async saveWechatMenu ({commit, state}, payload) {
    let res = await saveWechatMenu(payload)
    return res
  },
  async delWechatMenu ({commit, state}, appid) {
    let res = await delWechatMenu(appid)
    return res
  },
  async getWxNewsMediaid ({commit, state}, payload) {
    let res = await getWxNewsMediaid(payload)
    return res
  },
  async getWxNewsByMediaid ({commit, state}, payload) {
    let res = await getWxNewsByMediaid(payload)
    return res
  },
  async getWxMenuSource ({commit, state}, payload) {
    let res = await getWxMenuSource(payload)
    return res
  },
  // 自动回复
  async getSubscribeReply ({commit, state}, appid) {
    let res = await getSubscribeReply(appid)
    const mediaid = res.data.data.mediaid
    if (mediaid) {
      let res2 = await getSourceMoreInfo(res.data.data.mediaid)
      commit(type.UPDATE_SUBSCRIBE_REPLY, {...res.data.data, ...res2.data.data})
    } else {
      commit(type.UPDATE_SUBSCRIBE_REPLY, res.data.data)
    }
  },
  async setSubscribeReply ({commit, state}, payload) {
    let res = await setSubscribeReply(payload)
    return res
  },
  async setIfSubReply ({commit, state}, payload) {
    let res = await setIfSubReply(payload)
    commit(type.UPDATE_SUBSCRIBE_REPLY, {status: payload.status})
    return res
  },
  async getDefaultReply ({commit, state}, appid) {
    let res = await getDefaultReply(appid)
    const mediaid = res.data.data.mediaid
    if (mediaid) {
      let res2 = await getSourceMoreInfo(res.data.data.mediaid)
      commit(type.UPDATE_DEFAULT_REPLY, {...res.data.data, ...res2.data.data})
    } else {
      commit(type.UPDATE_DEFAULT_REPLY, res.data.data)
    }
  },
  async setDefaultReply ({commit, state}, payload) {
    let res = await setDefaultReply(payload)
    return res
  },
  async setIfDefaultReply ({commit, state}, payload) {
    let res = await setIfDefaultReply(payload)
    commit(type.UPDATE_DEFAULT_REPLY, {status: payload.status})
    return res
  },
  async getKeywordReply ({commit, state}, payload) {
    let res = await getKeywordReply(payload)
    commit(type.SET_KEYWORD_REPLY, res.data.data)
    return res
  },
  async setKeywordReply ({commit, state}, payload) {
    let res = await setKeywordReply(payload)
    return res
  },
  async delKeywordReply ({commit, state}, payload) {
    let res = await delKeywordReply(payload)
    return res
  },
  async getReplyTail ({commit, state}, appid) {
    let res = await getReplyTail(appid)
    commit(type.UPDATE_REPLY_TAIL, res.data.data)
    return res
  },
  async setReplyTail ({commit, state}, payload) {
    let res = await setReplyTail(payload)
    return res
  },
  async setIfReplyTail ({commit, state}, payload) {
    let res = await setIfReplyTail(payload)
    commit(type.UPDATE_REPLY_TAIL, {status: payload.status})
    return res
  },
  // 客服管理
  async getKefuList ({commit, state}, appid) {
    let res = await getKefuList(appid)
    commit(type.SET_KEFU_LIST, res.data.data.kf_list)
    return res
  },
  async getKefuListOnline ({commit, state}, appid) {
    let res = await getKefuListOnline(appid)
    commit(type.SET_KEFU_LIST, res.data.data.kf_list)
    return res
  },
  async addKefuAccount ({commit, state}, payload) {
    let res = await addKefuAccount(payload)
    return res
  },
  async bindKefuAccount ({commit, state}, payload) {
    let res = await bindKefuAccount(payload)
    return res
  },
  async setKefuAvatar ({commit, state}, payload) {
    let res = await setKefuAvatar(payload)
    return res
  },
  async setKefuInfo ({commit, state}, payload) {
    let res = await setKefuInfo(payload)
    return res
  },
  async delKefuAccount ({commit, state}, payload) {
    let res = await delKefuAccount(payload)
    if (res.data.code === 1) {
      commit(type.SET_KEFU_LIST, state.kefuList.filter(item => item.kf_account !== payload.kf_account))
    }
    return res
  },
  async setAdminInfo ({commit, state}, payload) {
    let res = await setAdminInfo(payload)
    return res
  }
}
const mutations = {
  // 公众号列表
  [type.GET_WECHAT_AUTH_URL] (state, payload) {
    state.wechatAuthUrl = payload
  },
  [type.SET_WECHAT_AUTH] (state, payload) {
    if (payload === 1) {
      state.wechatAuth = true
    } else {
      state.wechatAuth = false
    }
  },
  [type.SET_WECHAT_LIST] (state, payload) {
    state.wechatList = payload
  },
  // 自定义菜单
  [type.SET_WECHAT_MENU] (state, payload) {
    state.wechatMenu = payload
  },
  [type.SET_WECHAT_DETAIL] (state, payload) {
    state.wechatDetail = payload
  },
  [type.SET_MENU_INDEX] (state, payload) {
    state.menuIndex = payload
  },
  [type.SET_SUB_MENU_INDEX] (state, payload) {
    state.subMenuIndex = payload
  },
  [type.UPDATE_CUR_MENU] (state, payload) {
    const i = state.menuIndex
    const j = state.subMenuIndex
    if (j !== null) {
      state.wechatMenu[i].sub_button.splice(j, 1, Object.assign(state.wechatMenu[i].sub_button[j], payload))
    } else if (i !== null) {
      state.wechatMenu.splice(i, 1, Object.assign(state.wechatMenu[i], payload))
    }
  },
  [type.SET_CUR_MENU] (state, payload) {
    state.curMenu = payload
  },
  // 自动回复
  [type.UPDATE_SUBSCRIBE_REPLY] (state, payload) {
    if (payload) {
      state.subscribeReply = Object.assign({}, state.subscribeReply, payload)
    }
  },
  [type.UPDATE_DEFAULT_REPLY] (state, payload) {
    state.defaultReply = Object.assign({}, state.defaultReply, payload)
  },
  [type.SET_KEYWORD_REPLY] (state, payload) {
    state.keywordReply = payload
  },
  [type.UPDATE_KEYWORD_REPLY] (state, payload) {
    state.keywordReply.list = Object.assign({}, state.keywordReply, payload)
  },
  [type.UPDATE_REPLY_TAIL] (state, payload) {
    state.replyTail = Object.assign(state.replyTail, payload)
  },
  // 客服管理
  [type.SET_KEFU_LIST] (state, payload) {
    state.kefuList = payload
  }
}
export default {
  state,
  actions,
  mutations
}
